window.onload = function () {
    let new_height = parseInt(getCookie("new_height")) - 83;
    let content_panel = document.querySelectorAll('.contentPanel');
    for (let i = 0; i < content_panel.length; i++) {
        $(content_panel[i]).css('height', new_height);
    }
    $('#choose_date').click(function () {
        $('#choose_date_table').toggle("blind", {direction: "up"}, 300);
    });
    select_tr();
    state_style();
    person_info_fix();
}

function getCookie(cname) {
    let name = cname + "=";
    let ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i].trim();
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function my_suggest_find_new_key() {
    let key_words = $('#search_keyword').val();
    if (key_words == null) {
        return;
    }
    $.ajax({
        url: "../ajax/table.php",
        type: "POST",
        data: {my_suggest_key_words: key_words, my_suggest: "1"},
        success: function (result) {
            $(".contentPanel").html(result);
            state_style();
        }
    });
}

function my_impl_find_new_key() {
    let key_words = $('#search_keyword').val();
    if (key_words == null) {
        return;
    }
    $.ajax({
        url: "../ajax/table.php",
        type: "POST",
        data: {my_impl_key_words: key_words, my_impl: "1"},
        success: function (result) {
            $(".contentPanel").html(result);
            state_style();
        }
    });
}

function ci_find_new_key() {
    let key_words = $('#search_keyword').val();
    if (key_words == null) {
        return;
    }
    $.ajax({
        url: "../ajax/table.php",
        type: "POST",
        data: {ci_key_words: key_words, ci: "1"},
        success: function (result) {
            $(".contentPanel").html(result);
            state_style();
        }
    });
}

Date.prototype.format = function (fmt) {
    let o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };

    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (let k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }

    return fmt;
}

function show_all_find_key(dates) {
    let start_date = $('#StartDate').val();
    let end_date = $('#EndDate').val();
    let key_words = $('#search_keyword').val();
    let department = $('#department').val();
    let state = $('#State').val();
    let data1 = "";
    if (dates != ''){
        let now = new Date();
        let e_t = now.format("yyyy-MM-dd");
        now.setDate(now.getDate() - dates);
        let s_t = now.format("yyyy-MM-dd");
        data1 = data1 + "start_date=" + s_t + "&";
        data1 = data1 + "end_date=" + e_t + "&";
    }
    else {
        if (key_words != "") {
            data1 = data1 + "show_all_key_words=" + key_words + "&";
        }
        if (start_date != "" && end_date != "") {
            data1 = data1 + "start_date=" + start_date + "&";
            data1 = data1 + "end_date=" + end_date + "&";
            $("#SelectStartDate").html(start_date);
            $("#SelectEndDate").html(end_date);
        }
        if (start_date != "" && end_date == "") {
            alert("间隔日期填写不完整！")
            return
        }
        if (start_date == "" && end_date != "") {
            alert("间隔日期填写不完整！")
            return
        }
        if (department != "") {
            data1 = data1 + "department=" + department + "&";
        }
        if (state != "") {
            data1 = data1 + "state=" + state + "&";
        }
    }
    data1 = data1 + "show_all=1";
    let request = $.ajax({
        url: "../ajax/table.php",
        type: "POST",
        data: data1
    });

    request.done(function (result) {
        $(".contentPanel").html(result);
        state_style();
    });
    request.fail(function (jqXHR, textStatus) {
        alert("Request failed: " + textStatus);
    });
}

function add_sug() {
    $.ajax({
        url: "../dialog/dialog_border.php",
        type: "POST",
        data: {add_sug: 1},
        success: function (result) {
            $("#main", parent.document).after(result);
        }
    });
}

let sug_sn = "";
let sug_state = "";
let chosen_one = -1;

function suggest_detail() {
    if (!sug_sn) {
        alert("还未选择合理化建议！");
    } else {
        $.ajax({
            url: "../dialog/dialog_border.php",
            type: "POST",
            data: {
                sn: sug_sn,
                sug_detail:1
            },
            success: function (result) {
                $("#main", parent.document).after(result);
            }
        });
    }
}

function select_tr() {
    let trs = document.getElementsByClassName('sug_content_tr');
    if (trs.length > 0) {
        for (let i = 0; i < trs.length; i++) {
            $(trs[i]).mouseenter(function () {
                if (i != chosen_one) {
                    $(trs[i]).css('background-color', '#eee');
                }

            });
            $(trs[i]).mouseleave(function () {
                if (i != chosen_one) {
                    $(trs[i]).css('background-color', '#fff');
                }
            });
            $(trs[i]).click(function () {
                for (let ii = 0; ii < trs.length; ii++) {
                    $(trs[ii]).css('background-color', '#fff');
                    $(trs[ii]).css('color', '#000');
                }
                chosen_one = i;
                $(trs[i]).css('background-color', '#337ab7');
                $(trs[i]).css('color', '#fff');
                let td_sn = trs[i].querySelector('.SN');
                let td_state = trs[i].querySelector('.State');
                sug_sn = $(td_sn).html();
                sug_state = $(td_state).html();
            });
            $(trs[i]).dblclick(function () {
                for (let ii = 0; ii < trs.length; ii++) {
                    $(trs[ii]).css('background-color', '#fff');
                    $(trs[ii]).css('color', '#000');
                }
                chosen_one = i;
                $(trs[i]).css('background-color', '#337ab7');
                $(trs[i]).css('color', '#fff');
                let td_sn = trs[i].querySelector('.SN');
                let td_state = trs[i].querySelector('.State');
                sug_sn = $(td_sn).html();
                sug_state = $(td_state).html();
                suggest_detail();
            });
        }
    }
}

function state_style() {
    let states = document.getElementsByClassName("State");
    if(states.length <= 0){return;}
    for(let i = 0; i < states.length; i++){
        let text = $(states[i]).html();
        if(text.match("拒绝") != null){
            $(states[i]).css("background-color","#d9534f");
            $(states[i]).css("color","#ffffff");
        }
        else if(text.match("发布") != null){
            $(states[i]).css("background-color","#5cb85c");
            $(states[i]).css("color","#ffffff");
        }
        else if(text.match("建议完成") != null){
            $(states[i]).css("background-color","#f0ad4e");
            $(states[i]).css("color","#ffffff");
        }
        else if(text.match("建议审核通过") != null){
            $(states[i]).css("background-color","#5cb85c");
            $(states[i]).css("color","#ffffff");
        }
        else if(text.match("建议实施通过") != null){
            $(states[i]).css("background-color","#5C73B8");
            $(states[i]).css("color","#ffffff");
        }
        else{
            $(states[i]).css("background-color","#5bc0de");
            $(states[i]).css("color","#ffffff");
        }
    }
}

function person_info_fix() {
    $('#person_info_border').tabs();
    let new_height = parseInt(getCookie("new_height")) - 20;
    $('#person_info_border').css("height",new_height);
    let all_width = $('#person_info_border').width();
    let info_list_width = $('#info_list').width();
    let info_content_width = all_width - info_list_width;
    $('#info_content').css('width',info_content_width);
    let info_list_a = document.getElementsByClassName("ui-tabs-anchor");
    if(info_list_a.length > 0){
        for(let i = 0; i < info_list_a.length; i++){
            let a_width = $(info_list_a[i]).width();
            let a_padding_width = info_list_width - 3 - 20 - a_width;
            $(info_list_a[i]).css("padding-right",a_padding_width);
        }
    }
}

function update_connect_info_complete() {
    let Mobile = $("#Mobile").val();
    let Telephone = $("#Telephone").val();
    let Email = $("#Email").val();
    $.ajax({
        url: "../ajax/need_to_sql.php",
        type: "POST",
        data: {
            Mobile: Mobile,
            Telephone: Telephone,
            Email: Email,
            update_connect_info_complete: 1
        },
        success: function (result) {
            alert(result);
        }
    });
}

function update_password_complete() {
    let old_password = $('#old_password').val();
    let new_password = $('#new_password').val();
    let new_password_again = $('#new_password_again').val();
    $.ajax({
        url: "../ajax/need_to_sql.php",
        type: "POST",
        data: {
            old_password: old_password,
            new_password: new_password,
            new_password_again: new_password_again,
            update_password_complete: 1
        },
        success: function (result) {
            alert(result);
        }
    });
}

function ci_check() {
    if (!sug_sn) {
        alert("还未选择合理化建议！");
    } else {
        $.ajax({
            url: "../dialog/dialog_border.php",
            type: "POST",
            data: {
                sn: sug_sn,
                ci_check:1
            },
            success: function (result) {
                $("#main", parent.document).after(result);
            }
        });
    }
}

function auditor_check() {
    if (!sug_sn) {
        alert("还未选择合理化建议！");
    } else {
        $.ajax({
            url: "../dialog/dialog_border.php",
            type: "POST",
            data: {
                sn: sug_sn,
                state: sug_state,
                auditor_check:1
            },
            success: function (result) {
                $("#main", parent.document).after(result);
            }
        });
    }
}

function accept_want_complete(){
    if (!sug_sn) {
        alert("还未选择合理化建议！");
    } else {
        $.ajax({
            url: "../dialog/dialog_border.php",
            type: "POST",
            data: {
                sn: sug_sn,
                state: sug_state,
                accept_want_complete:1
            },
            success: function (result) {
                $("#main", parent.document).after(result);
            }
        });
    }
}

function accept_complete(){
    if (!sug_sn) {
        alert("还未选择合理化建议！");
    } else {
        $.ajax({
            url: "../dialog/dialog_border.php",
            type: "POST",
            data: {
                sn: sug_sn,
                state: sug_state,
                accept_complete:1
            },
            success: function (result) {
                $("#main", parent.document).after(result);
            }
        });
    }
}